package com.ax.business.common.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ax.business.common.dao.DocConfigMapper;
import com.ax.business.common.domain.DocConfig;
import com.ax.business.common.service.MakeOrderService;
import com.ax.business.common.util.DateWay;

@Service("MakeOrderService")
public class MakeOrderServiceImpl implements MakeOrderService {
	@Autowired
    DocConfigMapper docConfigMapper;
	
	/**
	 * 看医生开处方(模块:生成单号)
	 * 
	 * @param businessCode
	 *           
	 * @return String
	 */
	@Override
	public String getMakeOrderSer(String businessCode) {
		DocConfig docConfig = docConfigMapper.selectByPrimaryKey(businessCode,1);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("sysDatatime",DateWay.GetTime());
		map.put("businessCode",businessCode);
		Integer judge = docConfigMapper.JudgeMapper(map);
		//拼接各单号
		StringBuffer bf = new StringBuffer();
		if(judge>0){
			bf.append(docConfig.getPrefix());
			bf.append(DateWay.GetOrderTime());
			Integer nmber = docConfig.getCurNo()+1;
			bf.append(String.format("%0"+docConfig.getCurNoLength()+"d",nmber));  
			//修改数据库
			docConfig.setCurNo(nmber);
			docConfigMapper.updateByPrimaryKeySelective(docConfig);
		}else{
			//当日第一天个订单
			bf.append(docConfig.getPrefix());
			bf.append(DateWay.GetOrderTime());
			Integer nmber = 1;
			bf.append(String.format("%0"+docConfig.getCurNoLength()+"d",nmber));  
			//修改数据库
			docConfig.setCurNo(nmber);
			docConfigMapper.updateByPrimaryKeySelective(docConfig);
		}
		return bf.toString();
	}
}
